import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:apple_maps/apple_maps.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTestClick.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _appleMapsPlugin = AppleMaps();

  late MethodChannel _channel;
  late EventChannel _eventChannel;
  int viewId = 0;

  StreamSubscription? sub;

  @override
  void initState() {
    super.initState();

     sub = _appleMapsPlugin.getCompassStream().stream.listen((heading) {
      FFLog.info(heading.toString());
    });
  }

  @override
  void dispose() {
    sub?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('MapKit Example'),
        ),
        body: Center(
            child: UiKitView(
          viewType: "apple_maps.testView",
          creationParams: AppleMaps.creationParams(
            content: "flutter 传入的文本内容，在原生页面上展示2",
            annotationIcon: "mapheader",
            annotationIconSize: "80",
          ),
          creationParamsCodec: StandardMessageCodec(),
          onPlatformViewCreated: (id) {
            print("初始化完成");
            viewId = id;
            _channel = MethodChannel('apple_maps_view_$viewId');
            _eventChannel = EventChannel("apple_maps_sink_view_$viewId");
            _eventChannel.receiveBroadcastStream().listen((data) {
              String type = data["type"];
              print("收到原生 主动发送的消息 $data");
            });
          },
        )),
        floatingActionButton: FFLineContainer(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            FFTestClick(
              text: "单独开启指定监听",
              onClick: () {
                _appleMapsPlugin.startCompass();
              },
            ),

            FFTestClick(
              text: "监听设备朝向",
              onClick: () {
                startUpdatingHeading();
              },
            ),
            FFTestClick(
              text: "结束定位",
              onClick: () {
                stopUpdatingLocation();
              },
            ),
            FFTestClick(
              text: "开始定位",
              onClick: () {
                startUpdatingLocation();
              },
            ),
            FFTestClick(
              text: "点我授权",
              onClick: () {
                auth();
              },
            ),
          ],
        ),
      ),
    );
  }

  /**
   * @title 开始更新位置
   */
  startUpdatingLocation() {
    _channel.invokeMethod("startUpdatingLocation");
  }

  /**
   * @title 结束更新位置
   */
  stopUpdatingLocation() {
    _channel.invokeMethod("stopUpdatingLocation");
  }

  /**
   * @title 开始监听设备朝向
   */
  startUpdatingHeading() {
    _channel.invokeMethod("startUpdatingHeading");
  }

  /**
   * @title 结束监听设备朝向
   */
  stopUpdatingHeading() {
    _channel.invokeMethod("stopUpdatingHeading");
  }

  /**
   * @title 请求定位权限
   */
  auth() {
    _channel.invokeMethod("auth");
  }

  /**
   * @title 位置服务是否可用
   */
  Future<bool> locationServicesEnabled() async {
    String result = await _channel.invokeMethod("locationServicesEnabled") ?? "0";
    return result == "1";
  }
}
